﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.ComponentModel;
using System.Diagnostics;
using DarkLight.Model;

namespace DarkLight.WpfTest
{
    public class MainWindowVM : ObservableObject
    {
        #region Data
        private int _frames;
        private Stopwatch _timer;
        #endregion

        #region Constructors
        public MainWindowVM()
        {            
            _timer = new Stopwatch();
            _timer.Start();

            CameraPosition = new Spherical(300.0f, 0.0f, (float)Math.PI / 2);
            CameraTarget = Spherical.Zero;
            LightPosition = new Spherical(500.0f, (float)Math.PI / 4, (float)Math.PI / 2);
            
            _elements = new List<IPosition>();

            Planet planet = new Planet(50)
            {
                Name = "Earth",
                Position = Spherical.Zero,
                DiffuseCoefficient = 1,
                PhongCoefficient = 1,
                PhongExponent = 9,
                HasClouds = true,
                CloudsHeight = 0.01f,
                CloudsOpacity = 1.0f,
                CloudsSpeed = 1,
                HasAtmosphere = true,
                AtmosphereHeight = 0.05f,
                AtmosphereOpacity = 0.8f,
                AtmosphereColor = Colors.Blue,
                DiffuseTexture = Properties.Resources.Earth_Diffuse,
                GlowTexture = Properties.Resources.Earth_Glow,
                BumpTexture = Properties.Resources.Earth_Bump,
                SpecularTexture = Properties.Resources.Earth_Specular,
                CloudsTexture = Properties.Resources.Earth_Clouds,
                SpriteTexture = Properties.Resources.planet
            };

            _elements.Add(planet);

            CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
        }
        #endregion

        #region Public Members
        public int Test { get { return 60; } }

        public int Fps
        {
            get { return _fps; }
            set 
            {
                if (_fps != value)
                { 
                    _fps = value;
                    RaisePropertyChanged("Fps");
                }
            }
        }
        private int _fps;

        public Spherical CameraPosition
        {
            get { return _cameraPosition; }
            set 
            {
                if (!object.ReferenceEquals(_cameraPosition, value))
                {
                    _cameraPosition = value;
                    RaisePropertyChanged("CameraPosition");
                }
            }
        }
        private Spherical _cameraPosition;

        public Spherical CameraTarget
        {
            get { return _cameraTarget; }
            set 
            {
                if (!object.ReferenceEquals(_cameraTarget, value))
                {
                    _cameraTarget = value;
                    RaisePropertyChanged("CameraTarget");
                }
            }
        }
        private Spherical _cameraTarget;

        public Spherical LightPosition
        {
            get { return _lightPosition; }
            set
            {
                if (!object.ReferenceEquals(_lightPosition, value))
                {
                    _lightPosition = value;
                    RaisePropertyChanged("LightPosition");
                }
            }
        }
        private Spherical _lightPosition;

        public List<IPosition> Elements
        {
            get { return _elements; }
        }
        private List<IPosition> _elements;
        #endregion

        #region Event Handlers
        private void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            _frames++;
            if (_timer.ElapsedMilliseconds > 1000)
            {
                Fps = _frames;
                _frames = 0;
                _timer.Restart();
            }
        }
        #endregion
    }
}
